<template>
	 <ex-dialog v-model="visible" draggable :title="form.code"
	:width=700 :contentHeight=450>
	  <template #default>
		  <el-row :gutter="80" style="margin-top: 10px;width: 100%;">
			  <el-col :lg="24">
				  <el-form :model="form" :rules="button_rules" ref="dialogForm" label-width="100px" label-position="right">
					  <template v-if="dataEnabled == true">
						  <el-form-item label="任务名称" prop="name">
							  <ex-label :value="form.name "></ex-label>
						  </el-form-item>
						  <el-form-item v-if="dataEnabled" label="开始日期" prop="startDate">
							  <el-date-picker v-model="form.startDate"
											  value-format="YYYYMMDD"
											  type="date"
											  placeholder="开始日期"
											  date-format="YYYY-MM-DD"
											  :teleported="false"
											  :picker-options="startPickerOptions"
											  placement="right-end"/>
						  </el-form-item>
					  </template>
					  <template v-if="dataEnabled == false">
						  <el-form-item label="任务名称" prop="name">
							  <el-input disabled = true v-model="form.name" clearable placeholder="任务名称"></el-input>
						  </el-form-item>
					  </template>
					  <el-form-item label="结束日期" prop="endDate">
						  <el-date-picker v-model="form.endDate"
										  value-format="YYYYMMDD"
										  type="date"
										  date-format="YYYY-MM-DD"
										  placeholder="结束日期"
										  :teleported="false"
										  :picker-options="endPickerOptions"
										  placement="right-end"/>
					  </el-form-item>
					  <el-form-item label="说明" prop="description">
						  <el-input v-model="form.description" :rows="3" type="textarea" clearable placeholder="说明"></el-input>
					  </el-form-item>
				  </el-form>
			  </el-col>
		  </el-row>
	  </template>
	  <template #footer>
		<div style="flex: auto;text-align: center">
		  <el-button plain type="primary" @click="save" :loading="loading">保 存</el-button>
		  <el-button plain type="warning" @click="reset" :loading="loading">重 置</el-button>
		  <el-button plain type="info" @click="this.visible = false" :loading="loading">关 闭</el-button>
		</div>
	  </template>

	</ex-dialog>
  </template>

  <script>
  import scIconSelect from '@/components/scIconSelect'
  import ExRelTable from "@/components/exRelTable/index.vue";
  import ExLabel from "@/components/exLabel/index.vue";
  import ExSelect from "@/components/exSelect/index.vue";
  import exDialog from '@/components/exDialog';
  import ExTreeTable from "@/components/exTreeTable/index.vue";
  import moment from "moment/moment";

  export default {
	emits: ['success'],
	components: {
		ExTreeTable,
		ExSelect,
		ExLabel,
	  ExRelTable,
	  scIconSelect,
	  exDialog
	},
	props: {
	  menu: {
		type: Object, default: () => {
		}
	  },
	},
	data() {
	  return {
		visible: false,
		  entityName: "FamsAssetCheckTask",
		form: {
			id: "",
			name: "",
			taskType: "",
			createOrg: "",
			status: "",
			startDate:"",
			endDate:"",
			description:"",
		},
		button_rules: {
			startDate: [{required: true, message: "请选择开始日期", trigger: "blur"},],
			endDate: [{required: true, message: "请选择结束日期", trigger: "blur"},],
		},
		  dataEnabled:true,
		loading: false,
		  nowDate : moment(new Date()).format("YYYY-MM-DD"),
		oldForm: null,
		  startPickerOptions: {
			  disabledDate: (date) =>{
				  if (this.form.endDate) {
					  return date.getTime() > new Date(this.form.endDate).getTime();
				  }
				  return false;
			  }
		  },
		  endPickerOptions: {
			  disabledDate: (date) =>{
				  if (this.form.startDate) {
					  return date.getTime() < new Date(this.form.startDate).getTime();
				  }
				  return false;
			  }
		  },
	  }
	},
	  mounted() {

	},
	  onLoad() {},
	methods: {
		reset() {
			this.form = this.$TOOL.objCopy( this.oldForm);
		},
		//显示
		open(entity) {
			this.form = {};
			this.dataEnabled = true;
			this.form = Object.assign({}, entity);
			if(moment(this.form.startDate).isSameOrBefore(this.nowDate,'day')){
				this.dataEnabled = false;
			}
			this.oldForm = this.$TOOL.objCopy( this.form);
			this.visible = true;
		},
		//保存
		async save() {
			let valid  = await this.$refs.dialogForm.validate( valid => {return valid;});
			if (valid) {
				try {
					if(this.dataEnabled){
						//只有结束时间的时候，结束时间必须大于当前日期
						if(!moment(this.form.endDate).isSameOrAfter(this.nowDate,'day')){
							this.$message.warning("结束时间必须大于等于当前日期");
							return;
						}
					}else{
						//有开始日期的时候，开始日期必须小于等于当前日期
						if(moment(this.form.endDate).isBefore(this.form.startDate,'day')){
							this.$message.warning("结束时间必须大于开始日期");
							return;
						}
						if(!moment(this.form.endDate).isSameOrAfter(this.nowDate,'day')){
							this.$message.warning("结束时间必须大于等于当前日期");
							return;
						}
					}
					this.loading = true
					let rsp;
					let loginUser = this.$TOOL.data.get("USER_INFO");
					this.form.createOrg = loginUser.orgId;
					if (this.form.id) {
						rsp = await this.$API.asset.checkTaskApi.edit.post(this.form)
					} else {
						rsp = await this.$API.asset.checkTaskApi.add.post(this.form)
					}
					if (rsp.code === 2000) {
						this.$emit('success');
						this.$message.success(this.$t("code."+rsp.code));
						this.visible = false;
						this.$store.dispatch("commit",this.form.entityName);
					} else {
						this.$message.warning(rsp.message)
					}
				} finally {
					this.loading = false
				}
			}
		},
	}
  }
  </script>

  <style scoped>
  h2 {
	font-size: 17px;
	color: #3c4a54;
	padding: 0 0 30px 0;
  }

  [data-theme="dark"] h2 {
	color: #fff;
  }
  .label-center{
	  display: flex;
	  justify-content: center;
	  align-items: center;
  }
  </style>
